import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Demo {
    private ServerSocket serverSocket = null;

    public Demo(int p) throws IOException {
        serverSocket = new ServerSocket(p);
    }

    public void start() throws IOException {
        ExecutorService executorService = Executors.newCachedThreadPool();
        while (true) {
            Socket socket = serverSocket.accept();
            executorService.submit(() -> {
                try {
                    respond(socket);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            });
        }
    }

    private void respond(Socket socket) throws IOException {


            try (InputStream inputStream = socket.getInputStream();
                 OutputStream outputStream = socket.getOutputStream()) {

                Scanner sc = new Scanner(inputStream);
                PrintWriter printWriter = new PrintWriter(outputStream);
                while (true) {
                    while (!sc.hasNext()) {
                        break;
                    }
                    String s1 = sc.next();
                    String s2 = ressp(s1);
                    printWriter.println(s2);
                    printWriter.flush();
                    System.out.printf("[%s:%d] resq:%s res:%s", socket.getInetAddress(), socket.getPort(), s1, s2);
                }

            } catch (IOException e) {
                throw new RuntimeException(e);
            } finally {
                socket.close();
            }

    }

    private String ressp(String s1) {
        return s1;
    }

    public static void main(String[] args) throws IOException {
        Demo d = new Demo(9099);
        d.start();
    }
}
